package com.jackiepenghe.filetest

import com.jackiepenghe.filetest.databinding.ActivityMainBinding
import com.sscl.baselibrary.activity.BaseDataBindingAppCompatActivity
import com.sscl.baselibrary.files.FileUtil
import com.sscl.baselibrary.utils.BaseManager
import com.sscl.baselibrary.utils.ToastUtil
import java.io.File
import java.io.FileWriter

class MainActivity : BaseDataBindingAppCompatActivity<ActivityMainBinding>() {

    private lateinit var filePath: String

    /**
     * 在设置布局之前需要进行的操作
     */
    override fun doBeforeSetLayout() {

    }

    /**
     * 设置布局
     */
    override fun setLayout(): Int {
        return R.layout.activity_main
    }

    /**
     * 设置DataBinding
     * 可在此处设置binding的viewModel或观察者等操作
     */
    override fun setBinding() {

    }

    /**
     * 标题栏的返回按钮被按下的时候回调此方法
     */
    override fun titleBackClicked(): Boolean {
        return false
    }

    /**
     * 初始化控件数据
     */
    override fun initViewData() {
        filePath = File(FileUtil.sdCardDataDir, System.currentTimeMillis().toString() + ".txt").absolutePath

        binding.filePathTv.text = filePath
    }

    /**
     * 初始化其他数据
     */
    override fun initOtherData() {

    }

    /**
     * 在设置布局之后，进行其他操作之前，所需要初始化的数据
     */
    override fun doBeforeInitOthers() {

    }

    /**
     * 初始化事件
     */
    override fun initEvents() {
        binding.createFileBtn.setOnClickListener {
            val file = File(filePath)
            if (file.exists()) {
                if (file.isDirectory) {
                    FileUtil.deleteDirFiles(file)
                } else {
                    ToastUtil.toastLong(this@MainActivity, "文件已创建")
                    return@setOnClickListener
                }
            }
            val succeed = file.createNewFile()
            if (succeed) {
                ToastUtil.toastLong(this@MainActivity, "文件创建完成")
            } else {
                ToastUtil.toastLong(this@MainActivity, "文件创建失败")
            }
        }

        binding.writeDataToFileBtn.setOnClickListener {
            binding.writeDataToFileBtn.isFocusable = false
            binding.writeDataToFileBtn.isClickable = false
            BaseManager.threadFactory.newThread {
                val file = File(filePath)
                if (!file.exists()) {
                    ToastUtil.toastLong(this@MainActivity, "文件不存在")
                    BaseManager.handler.post {
                        binding.writeDataToFileBtn.isFocusable = true
                        binding.writeDataToFileBtn.isClickable = true
                    }
                    return@newThread
                }
                if (file.isDirectory) {
                    ToastUtil.toastLong(this@MainActivity, "不能对文件夹写入数据")
                    BaseManager.handler.post {
                        binding.writeDataToFileBtn.isFocusable = true
                        binding.writeDataToFileBtn.isClickable = true
                    }
                    return@newThread
                }
                val fileWriter = FileWriter(file, true)
                var count = 0
                while (true) {
                    fileWriter.append("测试 $count")
                        .append("\n")
                    count++
                    if (count > 1000) {
                        break
                    }
                }
                fileWriter.append("\n\n")
                fileWriter.flush()
                fileWriter.close()
                BaseManager.handler.post {
                    binding.writeDataToFileBtn.isFocusable = true
                    binding.writeDataToFileBtn.isClickable = true
                }
                ToastUtil.toastLong(this@MainActivity,R.string.write_data_succeed)
            }.start()
        }
        binding.deleteFileBtn.setOnClickListener {
            FileUtil.deleteDirFiles(FileUtil.sdCardDataDir)
            FileUtil.sdCardDataDir?.mkdirs()
            ToastUtil.toastLong(this@MainActivity,R.string.file_deleted)
        }
    }

    /**
     * 在最后进行的操作
     */
    override fun doAfterAll() {

    }
}